package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.GmListTable;
import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.InventoryUpdate;
import labox.innovation.gameserver.network.serverpackets.ItemList;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.L2EtcItemType;

/**
 * 丢弃物品协议
 *
 * @version $Revision: 1.11.2.1.2.7 $ $Date: 2005/04/02 21:25:21 $
 */
public final class RequestDropItem extends L2GameClientPacket {
	private static final String _C__79_REQUESTDROPITEM = "[C] 0x79 RequestDropItem";

	private static final Logger _log = LoggerFactory.getLogger(RequestDropItem.class.getName());

	private long _objectId; //丢弃的物品id

	private int _count; //丢弃的数目

	//丢在地上的 位置
	//现不需要
	private int _x;

	private int _y;

	@Override
	protected void readImpl() {
		_objectId = readLong();
		_count = readInt();
		_x = 0;
		_y = 0;

	}

	@Override
	protected void runImpl() {
		FuzePcInstance activeChar = getClient().getActiveChar();
		if (activeChar == null || activeChar.isDead())
			return;
		// Flood protect drop to avoid packet lag
		if (!activeChar.getFloodProtectors().getDropItem().tryPerformAction("drop item"))
			return;

		L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_objectId);

		if (item == null || _count == 0 || !activeChar.validateItemManipulation(_objectId, "drop") || (!Config.ALLOW_DISCARDITEM && !activeChar.isGM()) || (!item.isDropable() && !(activeChar.isGM() && Config.GM_TRADE_RESTRICTED_ITEMS))) {
			activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
			return;
		}
		if (item.getItemType() == L2EtcItemType.QUEST && !(activeChar.isGM() && Config.GM_TRADE_RESTRICTED_ITEMS)) {
			activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
			return;
		}

		if (_count > item.getCount()) {
			activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
			return;
		}

		if (Config.PLAYER_SPAWN_PROTECTION > 0 && activeChar.isInvul() && !activeChar.isGM()) {
			activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
			return;
		}

		//		if (_count < 0)
		//		{
		//			Util.handleIllegalPlayerAction(activeChar, "[RequestDropItem] Character " + activeChar.getName() + " of account " + activeChar.getAccountName() + " tried to drop item with oid " + _objectId + " but has count < 0!", Config.DEFAULT_PUNISH);
		//			return;
		//		}
		//		
		//		if (!item.isStackable() && _count > 1)
		//		{
		//			Util.handleIllegalPlayerAction(activeChar, "[RequestDropItem] Character " + activeChar.getName() + " of account " + activeChar.getAccountName() + " tried to drop non-stackable item with oid " + _objectId + " but has count > 1!", Config.DEFAULT_PUNISH);
		//			return;
		//		}

		if (!activeChar.getAccessLevel().allowTransaction()) {
			activeChar.sendMessage("Transactions are disable for your Access Level");
			activeChar.sendPacket(new SystemMessage(SystemMessageId.NOTHING_HAPPENED));
			return;
		}

		if (activeChar.isProcessingTransaction()) {
			activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_TRADE_DISCARD_DROP_ITEM_WHILE_IN_SHOPMODE));
			return;
		}

		// Cannot discard item that the skill is consuming
		//		if (activeChar.isCastingNow())
		//		{
		//			if (activeChar.getCurrentSkill() != null && activeChar.getCurrentSkill().getSkill().getItemConsumeId() == item.getItemId())
		//			{
		//				activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_THIS_ITEM));
		//				return;
		//			}
		//		}

		//		if (L2Item.TYPE2_QUEST == item.getItem().getType2() && !activeChar.isGM())
		//		{
		//			if (Config.DEBUG)
		//				_log.finest(activeChar.getObjectId() + ":player tried to drop quest item");
		//			activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_DISCARD_EXCHANGE_ITEM));
		//			return;
		//		}

		if (item.isEquipped()) {
			L2ItemInstance[] unequiped = activeChar.getInventory().unEquipItemInBodySlotAndRecord(item.getItem().getBodyPart());
			InventoryUpdate iu = new InventoryUpdate();
			for (L2ItemInstance itm : unequiped) {
				iu.addModifiedItem(itm);
			}
			activeChar.sendPacket(iu);
			activeChar.broadcastUserInfo();

			ItemList il = new ItemList(activeChar, true);
			activeChar.sendPacket(il);
		}

		L2ItemInstance dropedItem = activeChar.dropItem("Drop", _objectId, _count, _x, _y, null, false);
		
		if( dropedItem != null ){
			ItemTable.getInstance().destroyItem("角色丢弃", dropedItem, activeChar, null);
		}

		if (_log.isDebugEnabled())
			_log.debug("dropping " + _objectId + " item(" + _count + ") at: " + _x + " " + _y + " ");

		// activeChar.broadcastUserInfo();

		//		if (activeChar.isGM())
		//		{
		//			String target = (activeChar.getTarget() != null ? activeChar.getTarget().getName() : "no-target");
		//		}

//		if (dropedItem != null) {
//			String msg = "Character (" + activeChar.getName() + ") has dropped (" + dropedItem.getCount() + ")";
//			_log.warn(msg, Config.SERVER_ID);
//			GmListTable.broadcastMessageToGMs(msg);
//		}
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _C__79_REQUESTDROPITEM;
	}
}
